#!/usr/bin/python
# coding=utf-8
import time

import RPi.GPIO as GPIO

from config import Config


class Lock:

    def __init__(self, gpio):
        GPIO.setmode(GPIO.BCM)  # 使用BCM编码方式
        # 定义引脚
        self.gpio = int(gpio)
        # 设置引脚为输入和输出
        GPIO.setwarnings(False)
        GPIO.setup(self.gpio, GPIO.OUT)

    def openLock(self):
        '''
        开锁控制
        :return:
        '''
        GPIO.output(self.gpio, GPIO.HIGH)
        time.sleep(0.1)
        GPIO.output(self.gpio, GPIO.LOW)


if __name__ == '__main__':
    gpio = int(Config().read("Sys_Pins", "lock_pin"))
    # 调用简单demo
    try:
        lock = Lock(gpio)
        while True:
            lock.openLock()
            time.sleep(1)

    except KeyboardInterrupt:
        GPIO.cleanup()
